﻿var Sustainalytics;
(function (Sustainalytics) {
    (function (LRTM) {
        // service for interacting with Long Running Tasks
        var LrtmTaskService = (function () {
            // ctor
            function LrtmTaskService($http, $q) {
                this.$http = $http;
                this.$q = $q;
            }
            //#region POST
            // post
            LrtmTaskService.prototype.post = function (title, paramJson, taskType, isUpload, fileName, mimeType, files, uploadProgressFunc) {
                var _this = this;
                var ctx = this;

                var def = ctx.$q.defer();

                var url = '/lrtmserv/api/v1/task';

                var formData = new FormData();
                formData.append('title', title);
                formData.append('paramJson', paramJson);
                formData.append('taskType', taskType);

                // set whether current request is upload
                if (isUpload) {
                    formData.append('isUpload', isUpload);
                }

                // set file name
                if (isUpload && fileName) {
                    formData.append('fileName', fileName);
                }

                // set MIME type
                if (isUpload && mimeType) {
                    formData.append('mimeType', mimeType);
                }

                // add files
                if (isUpload && files) {
                    $.each(files, function (index) {
                        formData.append('fileUpload' + index, _this);
                    });
                }

                // ajax submit with upload if required
                var xhr = new XMLHttpRequest();
                xhr.open('POST', url, true);

                // track upload progress
                if (uploadProgressFunc && isUpload) {
                    xhr.upload.onprogress = function (e) {
                        if (e.lengthComputable) {
                            var percentComplete = (e.loaded / e.total) * 100;
                            uploadProgressFunc(percentComplete);
                        }
                    };
                }

                // track request completion
                xhr.onload = function () {
                    if (this.status == 200) {
                        def.resolve(this.response);
                    } else {
                        def.reject(this.response);
                    }
                };

                xhr.send(formData);

                return def.promise;
            };

            // run task without upload for current user
            LrtmTaskService.prototype.postTask = function (title, paramJson, taskType) {
                var ctx = this;

                // start task
                var promise = ctx.post(title, paramJson, taskType, false, null, null, null, null);

                return promise;
            };

            // run task with file upload for current user
            LrtmTaskService.prototype.postTaskWithUpload = function (title, paramJson, taskType, files, uploadProgressFunc) {
                var ctx = this;

                // start task + upload
                var promise = ctx.post(title, paramJson, taskType, true, null, null, files, uploadProgressFunc);

                return promise;
            };

            //#endregion
            //#region GET
            // get
            LrtmTaskService.prototype.get = function (taskInstanceId, overallStatus, getStatus, getResult) {
                var ctx = this;

                var url = '/lrtmserv/api/v1/task?';

                var param = $.param({
                    taskInstanceId: taskInstanceId,
                    overallStatus: overallStatus,
                    getStatus: getStatus,
                    getResult: getResult
                });

                var promise = ctx.$http.get(url + param);

                return promise;
            };

            // get overall status for current user
            LrtmTaskService.prototype.getOverallStatus = function () {
                var ctx = this;

                // get overall status
                var promise = ctx.get(null, true, null, null);

                return promise;
            };

            // get status for a specific task instance ID
            LrtmTaskService.prototype.getStatus = function (taskInstanceId) {
                var ctx = this;

                // get status
                var promise = ctx.get(taskInstanceId, null, true, null);

                return promise;
            };

            // get task instance result as an object
            LrtmTaskService.prototype.getResultAsObject = function (taskInstanceId) {
                var ctx = this;

                var promise = ctx.get(taskInstanceId, null, null, true);

                return promise;
            };

            // get task instance result as a download
            LrtmTaskService.prototype.getResultAsDownload = function (taskInstanceId) {
                var ctx = this;

                var url = '/lrtmserv/api/v1/task?';

                var param = $.param({
                    taskInstanceId: taskInstanceId,
                    getResult: true
                });

                url = url + param;

                window.open(url, '_self');
            };

            //#endregion
            //#region DELETE
            // delete
            LrtmTaskService.prototype.delete = function (taskInstanceId) {
                var ctx = this;

                var url = '/lrtmserv/api/v1/task/' + taskInstanceId;

                var promise = ctx.$http.delete(url);

                return promise;
            };
            return LrtmTaskService;
        })();
        LRTM.LrtmTaskService = LrtmTaskService;
    })(Sustainalytics.LRTM || (Sustainalytics.LRTM = {}));
    var LRTM = Sustainalytics.LRTM;
})(Sustainalytics || (Sustainalytics = {}));
//# sourceMappingURL=LrtmTaskService.js.map
